<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_manghe/model')->方法名();
*/
namespace jy_manghe;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function upopenlog($game=array()){
	    if(!$game['id']){
	        return false;
        }
		$openlog=pdo_get('jy_manghe_openlog',array('uniacid'=>$_SESSION['uniacid'],'gameid'=>$game['id'],'status'=>0),'','opentime desc');
		if(!$openlog['id'] || $openlog['opentime']<TIMESTAMP){
            $daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$game['pluginset']=json_decode($game['pluginset'],true);
            if($game['pluginset']['opentime'] && $game['pluginset']['opentime']){
                $opentime=strtotime($game['pluginset']['opentime']);
                if($opentime>time()){
                    return false;
                }
            }
			$he_ontime=$game['pluginset']['he_ontime'];
			if($he_ontime['start'] && $he_ontime['end']){
			    $day=date('Y-m-d');
                $starttime=strtotime($day.' '.$he_ontime['start']);
                $endtime=strtotime($day.' '.$he_ontime['end']);
                if($starttime>TIMESTAMP || $endtime<TIMESTAMP){
                    return false;
                }
            }
			//数据整分，整秒
            $lasttime=$openlog['opentime']?:TIMESTAMP;
            $lasttime=date('Y-m-d H:i',$lasttime);
            $lasttime=strtotime($lasttime);
			$he_i=$game['pluginset']['he_i']?:5;
			$addtime=$he_i*60?:300;
			$opentime=$lasttime+$addtime;
			if($opentime<TIMESTAMP){
                $opentime= TIMESTAMP+$addtime;
                $opentime=date('Y-m-d H:i',$opentime);
                $opentime=strtotime($opentime);
            }
			$dayno=pdo_count('jy_manghe_openlog',array('uniacid'=>$_SESSION['uniacid'],'gameid'=>$game['id'],'opentime >'=>$daytime),'id');
			$openlog=array(
				'uniacid'=>$_SESSION['uniacid'],
				'gameid'=>$game['id'],
				'opentype'=>$game['pluginset']['he_opentype'],
				'dayno'=>$dayno+1,
				'opentime'=>$opentime,
				'createtime'=>TIMESTAMP,
				'status'=>0,
			);
			pdo_insert('jy_manghe_openlog',$openlog);
			$openlog['id']=pdo_insertid();
		}
		if($openlog['id'] && $openlog['opentime']<TIMESTAMP){
			$this->kaijiang($openlog);
		}
		return $openlog;
	}
	//开奖
	public function kaijiang($openlog=array()){
		$game=pdo_get('jy_weishop_game',array('uniacid'=>$_SESSION['uniacid'],'id'=>$openlog['gameid']));
		$game['pluginset']=json_decode($game['pluginset'],true);
		$game['pluginset']['he_onejifen']=$game['pluginset']['he_onejifen']?:1;
		$game['reward']=json_decode($game['reward'],true);
		$alljifen=pdo_getsum('jy_manghe_feelog',array('openlogid'=>$openlog['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
		foreach($game['reward'] as $k=>&$rv){
			$rv['rewardkey']=$k;
			$rv['isjifen']=pdo_getsum('jy_manghe_feelog',array('rewardkey'=>$k,'openlogid'=>$openlog['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
			if($rv['isjifen'] && $alljifen){
                $rv['newrate']=$rv['isjifen']*100/$alljifen;
            }else{
                $rv['newrate']=0;
            }
			if($game['pluginset']['he_maxopen']>0){
                $rv['lastmaxopen']=$game['pluginset']['he_maxopen']-$rv['newrate'];
                $rv['lastmaxopen']=$rv['lastmaxopen']>0?$rv['lastmaxopen']:0-$rv['lastmaxopen'];
            }
			$rv['rand']=rand(0,100);
		}
		$jifenreward=$levelreward=$game['reward'];
		//最大奖励开奖
		if($game['pluginset']['he_opentype']==2){
			paixu($levelreward,'level','desc');
			paixu($jifenreward,'isjifen','desc');
			foreach($jifenreward as $k=>&$jv){
				$jv['newkey']=$levelreward[$k]['rewardkey'];
			}
		}elseif($game['pluginset']['he_opentype']==3){
			paixu($levelreward,'level','desc');
			paixu($jifenreward,'isjifen','asc');
			foreach($jifenreward as $k=>&$jv){
				$jv['newkey']=$levelreward[$k]['rewardkey'];
			}
		}elseif($game['pluginset']['he_opentype']==4){
            paixu($levelreward,'level','desc');
            paixu($jifenreward,'lastmaxopen','asc');
            foreach($jifenreward as $k=>&$jv){
                $jv['newkey']=$levelreward[$k]['rewardkey'];
            }
        }elseif($game['pluginset']['he_opentype']==5){
		    if($openlog['setreward']){
                $jifenreward=json_decode($openlog['setreward'],true);
            }else{
                paixu($jifenreward,'rand','asc');
                foreach($jifenreward as $k=>&$jv){
                    $jv['newkey']=$game['reward'][$k]['rewardkey'];
                }
            }
        }else{
			paixu($jifenreward,'rand','asc');
			foreach($jifenreward as $k=>&$jv){
				$jv['newkey']=$game['reward'][$k]['rewardkey'];
			}
		}
		//发奖
        paixu($levelreward,'level','desc');
		foreach($jifenreward as &$jjv){
            $jjv['newkey']=$jjv['newkey']?:0;
			$mids=pdo_getall('jy_manghe_feelog',array('rewardkey'=>$jjv['rewardkey'],'openlogid'=>$openlog['id'],'uniacid'=>$_SESSION['uniacid']),array('mid'),'mid');
            $jjv['jiangfee']=0;
			if($mids){
				$nowreward=$game['reward'][$jjv['newkey']];
				$day=date('Y-m-d',$openlog['opentime']);
				$mids=array_keys($mids);
                //file_put_contents(ROOT_D.'/3.TXT',ijson_encode($nowreward)."~~\n",FILE_APPEND);
				foreach($mids as $mid){
					$isjifen=pdo_getsum('jy_manghe_feelog',array('mid'=>$mid,'rewardkey'=>$jjv['rewardkey'],'openlogid'=>$openlog['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
					$getfeenum=intval($isjifen/$game['pluginset']['he_onejifen']);
					$fee=$getfeenum*$nowreward['jifen'];
					if($fee>0 && $nowreward['type']){
						model('core/member')->member_credit($mid,'credit'.$nowreward['type'],$fee,true,'jy_manghe',$day.'第'.$openlog['dayno'].'期盲盒开奖',$openlog['gameid']);
                        $jjv['jiangfee'] +=$fee;
					}
                    //最高奖
                    if($jjv['newkey']==$levelreward[0]['rewardkey']){
                        $maxlog=array(
                            'mid'=>$mid,
                            'isjifen'=>$isjifen,
                            'openlogid'=>$openlog['id'],
                            'gameid'=>$openlog['gameid'],
                            'fee'=>$fee,
                            'credittype'=>$nowreward['type'],
                        );
                        $this->add_maxlog($maxlog);
                    }
				}
			}
		}
        pdo_update('jy_manghe_feelog',array('status'=>1),array('openlogid'=>$openlog['id'],'uniacid'=>$_SESSION['uniacid']));
        pdo_update('jy_manghe_openlog',array('isfee'=>$alljifen,'status'=>1,'reward'=>ijson_encode($jifenreward)),array('uniacid'=>$_SESSION['uniacid'],'id'=>$openlog['id']));
		return true;
	}
	public function check_buy($game,$onereward=array(),$member=array()){
		if(!$game){
			exi('活动不存在','error');
		}
		$game['pluginset']=json_decode($game['pluginset'],true);
		$game['pluginset']['he_onenum']=$game['pluginset']['he_onenum']?:0;
		$game['pluginset']['he_m']=$game['pluginset']['he_m']?:0;
		$game['pluginset']['he_onejifen']=$game['pluginset']['he_onejifen']?:1;
		if($game['pluginset']['he_minjifen'] && $onereward['addjifen']<$game['pluginset']['he_minjifen']){
			exi('最少投入积分'.$game['pluginset']['he_minjifen'].'个！','error');
		}
		$qiuyue=$onereward['addjifen']%$game['pluginset']['he_onejifen'];
		if($qiuyue){
			exi('投入积分必须是'.$game['pluginset']['he_onejifen'].'的倍数！','error');
		}
		$openlogid=$this->session('openlogid');
		$openlog=pdo_get('jy_manghe_openlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$openlogid));
		if($openlog['opentime']<TIMESTAMP+$game['pluginset']['he_m']){
			exi('每期开奖前'.$game['pluginset']['he_m'].'秒,不能参与！','error','/game//id='.$game['id']);
		}
		$game['reward']=json_decode($game['reward'],true);
		$ishe_onenum=0;
		foreach($game['reward'] as $k=>&$rv){
			$rv['isjifen']=pdo_getsum('jy_manghe_feelog',array('rewardkey'=>$k,'openlogid'=>$openlogid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'gameid'=>$game['id']),'fee');
			$rv['isjifen']=$rv['isjifen']?:0;
			if($rv['isjifen']>0){
				$ishe_onenum++;
				if($onereward['key']==$k){
					$isjifen=$rv['isjifen'];
					$istou=true;
				}
			}
		}
		if(!$istou && $ishe_onenum>=$game['pluginset']['he_onenum'] && $game['pluginset']['he_onenum']>0){
			exi('每期最多投'.$game['pluginset']['he_onenum'].'个盲盒！','error');
		}
		if($isjifen+$onereward['addjifen']>=$game['pluginset']['he_maxjifen'] && $game['pluginset']['he_maxjifen']>0){
			exi('每个盲盒最多投'.$game['pluginset']['he_maxjifen'].'个积分！','error');
		}
		return true;
	}
	public function add_feelog($item,$type=0){
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$item['mid']?:$_SESSION['uid'],
			'gameid'=>$item['gameid'],
			'openlogid'=>$item['openlogid'],
			'fee'=>$item['fee'],
			'type'=>$type,
			'rewardkey'=>$item['rewardkey'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		$this->insert('feelog',$data);
		$data['id']=pdo_insertid();
		if(!$type){
			$event=array(
				'type'=>'add',
				'log'=>$data,
			);
			$this->publish('add_feelog',$event);
		}
		return true;
	}
    public function add_maxlog($item,$type=0){
        $data=array(
            'uniacid'=>$_SESSION['uniacid'],
            'mid'=>$item['mid']?:$_SESSION['uid'],
            'gameid'=>$item['gameid'],
            'openlogid'=>$item['openlogid'],
            'fee'=>$item['fee'],
            'type'=>$type,
            'isjifen'=>$item['isjifen'],
            'credittype'=>$item['credittype'],
            'createtime'=>TIMESTAMP,
            'status'=>0,
        );
        $this->insert('maxlog',$data);
        return true;
    }
}
?>